import React, {Component} from 'react';
import app from '../app';
import {BrowserRouter as Router, Redirect, Route, Switch,} from 'react-router-dom';
import {Layout} from 'antd';
import Home from "./Home";  //登录成功后的页面
import Login from './Login';  //登录页面
import {config} from '../config';
import 'antd/dist/antd.css';

const path = config.path;
const fpath = config.fPath;

// 登陆界面
class Index extends Component {
    render() {
        const isAuth = app.getToken();
        const screenHeight = document.body.clientWidth;
        return (
            <Router>
                <Layout style={{height: '100%'}}>
                    <Switch>
                        <Route path={path + "/login"} component={Login} history={this.props.history}/>
                        <Route path={path + "/home"} render={(props) => {
                            const component = isAuth ? (<Home {...props} />) : (<Login/>);
                            return component;
                        }} history={this.props.history}/>
                        <Route path={"/"} component={Login}/>
                    </Switch>
                </Layout>
            </Router>
        )
    }
}

let RouteWithSubRoutes = (route) => (
    <Route path={route.path} render={props => (
        // 把自路由向下传递来达到嵌套。
        <route.component {...props} routes={route.routes}/>
    )}/>
);

export {Index, RouteWithSubRoutes};